package com.example.reactor.official.flux;

import reactor.core.publisher.Flux;
import reactor.core.scheduler.Schedulers;

import java.time.Duration;
import java.util.stream.IntStream;

/**
 * @author zpMeng
 * @since 3/31/2025
 */
public class FluxUtils {

    /**
     * 15.6 ms
     */
    private static final int DEFAULT_DELAY_INTERVAL_IN_WINDOWS = 20;

    public static Flux<Integer> prepareItemsWithInterval() {
        return prepareItemsWithInterval(100, DEFAULT_DELAY_INTERVAL_IN_WINDOWS);
    }

    public static Flux<Integer> prepareItemsWithInterval(int size, int millis) {
        return Flux
                .fromStream(IntStream.range(0, size).boxed())
                .delayElements(Duration.ofMillis(millis))
                .publishOn(Schedulers.boundedElastic());
    }
}
